package cn.buaa.beijing.fragment;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

import cn.buaa.beijing.R;
import cn.buaa.beijing.base.BasePager;
import cn.buaa.beijing.base.impl.GovAffairsPager;
import cn.buaa.beijing.base.impl.HomePager;
import cn.buaa.beijing.base.impl.NewsCenterPager;
import cn.buaa.beijing.base.impl.SettingPager;
import cn.buaa.beijing.base.impl.SmartServicePager;

/**
 * 主页内容
 * 
 * @author Kevin
 * 
 */
public class ContentFragment extends BaseFragment {
	ArrayList<BasePager> mPagerList;

	@ViewInject(R.id.rg_group)
	private RadioGroup radioGroup;

	@ViewInject(R.id.vp_content)
	private ViewPager mViewPager;

	@Override
	public View initViews() {
		View view = View.inflate(mActivity, R.layout.fragment_content, null);
		//radioGroup = (RadioGroup) view.findViewById(R.id.rg_group);
		ViewUtils.inject(this,view);
		return view;
	}

	@Override
	public void initData() {
		super.initData();
		//默认选中首页
		radioGroup.check(R.id.rb_home);

		// 初始化5个子页面
		mPagerList = new ArrayList<BasePager>();
//		for (int i = 0; i < 5; i++) {
//			BasePager pager = new BasePager(mActivity);
//			mPagerList.add(pager);
//		}

		mPagerList.add(new HomePager(mActivity));
		mPagerList.add(new NewsCenterPager(mActivity));
		mPagerList.add(new SmartServicePager(mActivity));
		mPagerList.add(new GovAffairsPager(mActivity));
		mPagerList.add(new SettingPager(mActivity));

		mViewPager.setAdapter(new ContentAdapter());

		mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
				mPagerList.get(position).initData();
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});
		mPagerList.get(0).initData();
		// 监听RadioGroup的选择事件
		radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
					case R.id.rb_home:
						// mViewPager.setCurrentItem(0);// 设置当前页面
						mViewPager.setCurrentItem(0, false);// 去掉切换页面的动画
						break;
					case R.id.rb_news:
						mViewPager.setCurrentItem(1, false);// 设置当前页面
						break;
					case R.id.rb_smart:
						mViewPager.setCurrentItem(2, false);// 设置当前页面
						break;
					case R.id.rb_gov:
						mViewPager.setCurrentItem(3, false);// 设置当前页面
						break;
					case R.id.rb_setting:
						mViewPager.setCurrentItem(4, false);// 设置当前页面
						break;

					default:
						break;
				}
			}
		});
	}

	public NewsCenterPager getNewsCenterPager() {
		return (NewsCenterPager) mPagerList.get(1);
	}

	class ContentAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return mPagerList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			BasePager pager = mPagerList.get(position);
			container.addView(pager.mRootView);
			//pager.initData(); 不能在此处调用ViewPager的initData方法 否者会加载左右两页 导致关闭侧边栏失效
			return pager.mRootView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

	}
}
